﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions;

namespace communicateWithKIT
{
    public partial class Form1 : Form
    {
        static string data_from_COMport = "";
        static int flag_connect = 0;
        static int flag_adc_read = 0;
        static int flag_adc_max_alarm = 0;
        static int flag_adc_min_alarm = 0;
        static int flag_file_dir = 0;
        static int flag_file_open = 0;
        public Form1()
        {
            InitializeComponent();
            //mount serial port names
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                listBox_comport.Items.Add(port);
            }
            listBox_comport.SetSelected(0, true);
            radioButton_led_shr.Checked = true;
            radioButton_dac_const.Checked = true;
            textBox_led_delay.Text = "1000";
            textBox_dac_freq.Text = "50";
            textBox_dac_Toff.Text = "1000";
            textBox_dac_Ton.Text = "1000";
            textBox_adc_alarm_delay.Text = "10";
            textBox_adc_alarm_max.Text = "50";
            textBox_adc_alarm_min.Text = "50";
        }

        private void button_connectCom_Click(object sender, EventArgs e)
        {
            if (COMport.IsOpen)
            {
                COMport.Close();
                button_connectCom.Text = "CONNECT";
                label_statusCOM.Text = "not connect";
                label_statusCOM.ForeColor = Color.Red;
            }
            else
            {
                COMport = new SerialPort(listBox_comport.SelectedItem.ToString(), 115200, Parity.None, 8);
                try
                {
                    COMport.Open();
                    COMport.DataReceived += new SerialDataReceivedEventHandler(COMport_DataReceived);
                    button_connectCom.Text = "DISCONNECT";
                    label_statusCOM.Text = "Connected";
                    label_statusCOM.ForeColor = Color.DarkGreen;
                    COMport.WriteLine("CONNECT_SOFTWARE\r\n");
                }
                catch (UnauthorizedAccessException)
                {
                    label_statusCOM.Text = "problem! choose another";
                    label_statusCOM.ForeColor = Color.Red;
                }             
            }
        }

        private void COMport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            this.Invoke(new EventHandler(DoUpdate));
        }
        private void DoUpdate(object s, EventArgs e)
        {
            data_from_COMport = data_from_COMport + COMport.ReadExisting();
            COMport.DiscardInBuffer();
            if (flag_adc_read == 1)
            {
                textBox_adc_res.Text = data_from_COMport;
                data_from_COMport = "";
                flag_adc_read = 0;
            }
            if (flag_adc_max_alarm == 1)
            {
                textBox_adc_alarm_max.Text = data_from_COMport;
                data_from_COMport = "";
                flag_adc_max_alarm = 0;
            }
            if (flag_adc_min_alarm == 1)
            {
                textBox_adc_alarm_min.Text = data_from_COMport;
                data_from_COMport = "";
                flag_adc_min_alarm = 0;
            }
            if (flag_file_dir == 1)
            {
                listBox_fileList.Items.Clear();
                string[] elements = Regex.Split(data_from_COMport, "\r\n");
                data_from_COMport = "";
                foreach (string element in elements)
                {
                    if (element != "")
                    {
                        listBox_fileList.Items.Add(element);
                    }
                }
                listBox_fileList.SetSelected(0, true);
                flag_file_dir = 0;
            }
            if (flag_file_open == 1)
            {
                richTextBox_fileContent.AppendText(data_from_COMport);
                data_from_COMport = "";
            }
        }

        private void button_setLed_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_";
            if (radioButton_led_shr.Checked)
            {
                request_to_send = request_to_send + "led_shr_";
            }
            else if(radioButton_led_shl.Checked)
            {
                request_to_send = request_to_send + "led_shl_";
            }
            else if(radioButton_led_inc_lr.Checked)
            {
                request_to_send = request_to_send + "led_inc_lr_";
            }
            else if (radioButton_led_inc_rl.Checked)
            {
                request_to_send = request_to_send + "led_inc_rl_";
            }
            else if (radioButton_led_dec_lr.Checked)
            {
                request_to_send = request_to_send + "led_dec_lr_";
            }
            else if (radioButton_led_dec_rl.Checked)
            {
                request_to_send = request_to_send + "led_dec_rl_";
            }
            else if(radioButton_led_incdec_lr.Checked)
            {
                request_to_send = request_to_send + "led_incdec_lr_";
            }
            else if (radioButton_led_incdec_rl.Checked)
            {
                request_to_send = request_to_send + "led_incdec_rl_";
            }
            request_to_send = request_to_send + textBox_led_delay.Text + "\r\n";
            MessageBox.Show(request_to_send);
            COMport.WriteLine(request_to_send);
        }

        private void button_dac_gen_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_";
            if (radioButton_dac_const.Checked)
            {
                request_to_send = request_to_send + "dac_const_";
            }
            else if(radioButton_dac_sine.Checked)
            {
                request_to_send = request_to_send + "dac_sine_";
            }
            else if(radioButton_dac_triangular.Checked)
            {
                request_to_send = request_to_send + "dac_triangular_";
            }
            else if (radioButton_dac_square.Checked)
            {
                request_to_send = request_to_send + "dac_square_";
            }
            else if (radioButton_dac_squareCustom.Checked)
            {
                request_to_send = request_to_send + "dac_squarecustom_";
            }
            if (radioButton_dac_squareCustom.Checked)
            {
                request_to_send = request_to_send + textBox_dac_Ton.Text + "_" + textBox_dac_Toff.Text;
            }
            else
            {
                request_to_send = request_to_send + textBox_dac_freq.Text;
            }
            request_to_send = request_to_send + "\r\n";
            COMport.WriteLine(request_to_send);
        }

        private void radioButton_dac_const_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_dac_const.Checked)
            {
                label_freq_percentVDD.Text = "% of VDD";
            }
            else
            {
                label_freq_percentVDD.Text = "frequency (Hz)";
            }
        }

        private void button_get_adc_res_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_adc_read\r\n";
            COMport.WriteLine(request_to_send);
            Thread.Sleep(200);
            flag_adc_read = 1;
            flag_file_open = 0;
        }

        private void button_get_adc_alarm_min_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_adc_get_alarm_min\r\n";
            COMport.WriteLine(request_to_send);
            Thread.Sleep(200);
            flag_adc_min_alarm = 1;
            flag_file_open = 0;
        }

        private void button_get_adc_alarm_max_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_adc_get_alarm_max\r\n";
            COMport.WriteLine(request_to_send);
            Thread.Sleep(200);
            flag_adc_max_alarm = 1;
            flag_file_open = 0;
        }

        private void button_set_adc_alarm_min_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_adc_";
            request_to_send = request_to_send + "set_alarm_min_" + textBox_adc_alarm_min.Text;
            request_to_send = request_to_send + "\r\n";
            COMport.WriteLine(request_to_send);
        }

        private void button_set_adc_alarm_max_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_adc_";
            request_to_send = request_to_send + "set_alarm_max_" + textBox_adc_alarm_max.Text;
            request_to_send = request_to_send + "\r\n";
            COMport.WriteLine(request_to_send);
        }

        private void button_active_adc_alarm_Click(object sender, EventArgs e)
        {
            string request_to_send = "CMD_adc_";
            request_to_send = request_to_send + "act_alarm_" + textBox_adc_alarm_delay.Text;
            request_to_send = request_to_send + "\r\n";
            COMport.WriteLine(request_to_send);
        }

        private void button_getfilelist_Click(object sender, EventArgs e)
        {
            //listBox_fileList.Items.Clear();
            string request_to_send = "CMD_file_";
            request_to_send = request_to_send + "dir" + "\r\n";
            COMport.WriteLine(request_to_send);
            Thread.Sleep(200);
            flag_file_dir = 1;
            flag_file_open = 0;
        }

        private void listBox_fileList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string item = listBox_fileList.SelectedItem.ToString();
            bool status = item.Contains(" <F>");
            richTextBox_fileContent.Text = "";
            string request_to_send = "CMD_file_";
            request_to_send = request_to_send + "open_" + item;
            request_to_send = request_to_send + "\r\n";
            COMport.WriteLine(request_to_send);
            Thread.Sleep(200);
            if (status == true)
            {
                flag_file_dir = 1;
            }
            else
            {
                flag_file_open = 1;
            }
        }
    }
}
